createMenu();
 
function createMenu() {
    var parentId = chrome.contextMenus.create({"title": "Kipin Pro", 
                            "contexts":["link"],
                            "onclick": linkMenu,
                            "targetUrlPatterns": ["http://forums.kipin.net/memberlist.php?mode=viewprofile&u=*"]});
    chrome.contextMenus.create({"title": "Send PM", 
                            "contexts":["link"],
                            "parentId": parentId,
                            "onclick": sendPm});
    chrome.contextMenus.create({"title": "Send AIM", 
                            "contexts":["link"],
                            "parentId": parentId,
                            "onclick": sendAim});                            
    chrome.contextMenus.create({"title": "Search Posts", 
                            "contexts":["link"],
                            "parentId": parentId,
                            "onclick": searchPosts});                           
                                                        
}

function linkMenu(info, tab) {
    
}

function sendPm(info, tab) {
    var userId = getUserId(info.linkUrl);
    if(userId != -1) {
        var url = 'http://forums.kipin.net/ucp.php?i=pm&mode=compose&u=' + userId;
        sendUrlRequest(url);
    }
}

function sendAim(info, tab) {
    var userId = getUserId(info.linkUrl);
    if(userId != -1) {
        var url = 'http://forums.kipin.net/memberlist.php?mode=contact&action=aim&u=' + userId;
        sendUrlRequest(url);
    }
}

function searchPosts(info, tab) {
    var userId = getUserId(info.linkUrl);
    if(userId != -1) {
        var url = 'http://forums.kipin.net/search.php?author_id=' + userId + 'sr=posts';
        sendUrlRequest(url);
    }
}

function renameUser(info, tab) {
    
}

function addFriend(info, tab) {
    var userId = getUserId(info.linkUrl);
    if(userId != -1) {
        // todo: lame this uses the username not id, gotta get that
        var url = 'http://forums.kipin.net/ucp.php?i=zebra&add=' + userId;
        //sendUrlRequest(url);
    }    
}

function addFoe(info, tab) {
    
}

function getUserId(url) {
    var usreId = -1;
    var searchString = 'memberlist.php?mode=viewprofile&u=';
    var pos = url.indexOf(searchString);
    if(pos != -1) {
        userId = url.substring(pos+searchString.length);
    }
    return userId;
}

// send message to content script to go to url
function sendUrlRequest(url) {
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, {url: url});
    });    
}
